package com.suyafei.controller;


import com.suyafei.enums.BizCodeEnum;
import com.suyafei.request.UserLoginRequest;
import com.suyafei.request.UserRegisterRequest;
import com.suyafei.service.FileService;
import com.suyafei.service.UserService;
import com.suyafei.util.JsonData;
import com.suyafei.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author suyafei
 * @since 2021-01-28
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    @ApiOperation("上传头像")
    @PostMapping(value = "upload")
    public JsonData uploadUserImg(
            @ApiParam(value = "文件上传")
            @RequestPart("file") MultipartFile file) {

        String result = fileService.uploadUserImg(file);

        return result != null ? JsonData.buildSuccess(result) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    /**
     * 用户注册
     *
     * @param registerRequest
     * @return
     */
    @ApiOperation("注册用户")
    @PostMapping("register")
    public JsonData register(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest registerRequest) {


        JsonData jsonData = userService.register(registerRequest);
        return jsonData;
    }

    @ApiOperation("用户登录")
    @PostMapping("login")
    public JsonData login(
            @ApiParam("用户登录对象")
            @RequestBody UserLoginRequest userLoginRequest
    ) {
        JsonData jsonData = userService.login(userLoginRequest);

        return jsonData;
    }


    @ApiOperation("个人信息查询")
    @GetMapping("detail")
    public JsonData detail() {
        UserVO userVO = userService.findUserDetail();
        return JsonData.buildSuccess(userVO);
    }
}

